<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class aboutController extends Controller
{
    //查询数据
    public function index()
    {

        //查询并分页
        //$res = DB::table('user')->where('u_member','1')->paginate(2);

        $res = DB::select('select `id`,`a_icon`,`a_name`,`a_tag`,`a_module`,`a_display` from `about`');

        //var_dump($res);

        return view('admin/admabout/admab', ['res' => $res]);
    }




    //添加
    public function add(Request $request)
    {



        $file = $request->File();

        //判断是否有头像上传
        if (!empty($file)){


            var_dump($file);
            $file = $request->File();
            $key = key($file);



            if ($_FILES[$key]['error'] > 0) {

                switch ($_FILES[$key]['error']) {
                    case '1':
                        $msg = '孩子, 回家重新上传, 文件太大了, 不要超过2M';
                        return back();
                        break;
                    case '2':
                        $msg = '请打开F12, 删除 MAX_FILE_SIZE , 即可正常上传';
                        return back();
                        break;
                    case '3':
                        $msg = '请查看网线是否连接好';
                        return back();
                        break;
                    case '4':
                        $msg = '请不要调戏我, 上传一个给我';
                        return back();
                        break;
                    case '6':
                        $msg = '请联系网管, 晚上啥时候约一下, 连个目录都不给我';
                        return back();
                        break;
                    case '7':
                        $msg = '再联系网管, 你想什么时候死, 说一声, 连个权限都不给我';
                        return back();
                        break;
                }
            }
            if (!is_uploaded_file($_FILES[$key]['tmp_name'])) {
                return back();
            }

            // 判断文件类型
            $ext = $request->file('a_icon')->getClientOriginalExtension();

            if (!in_array($ext, array('jpg', 'png', 'jpeg'))) {
                //('不合法的文件类型');
                return back();
            }

            // 移动文件到指定位置

            $path = './uploads/' . date('Y/m/d');
            $name = date('Ymd') . uniqid();
            $ext = $request->file('a_icon')->getClientOriginalExtension();
            $filename = $name . '.' . $ext;
            $request->file('a_icon')->move($path, $filename);

            //获取图片存储的路径
            $newfilename = $path.'/'.$filename;
            $arr = ltrim($newfilename,'.');

            $arr = array(
                'a_name' => $request->a_name,
                'a_tag' => $request->a_tag,
                'a_module' => $request->a_module,
                'a_display' => $request->a_dispaly,
                'a_icon' => $arr
            );

            $res = DB::table('about')->insert($arr);

            if ($res){

                return redirect('/notice')->with(['message'=>'添加成功','url' =>'adm/addabout', 'jumpTime'=>3,'status'=>true]);

            }

        }else{


            //获取数据
            $arr = array(
                'a_name' => $request->a_name,
                'a_tag' => $request->a_tag,
                'a_module' => $request->a_module,
                'a_display' => $request->a_dispaly
            );

            $res = DB::table('about')->insert($arr);

            if ($res){

                return redirect('/notice')->with(['message'=>'添加成功','url' =>'adm/addabout', 'jumpTime'=>3,'status'=>true]);
            }else{

                echo '添加失败';
            }


        }

        //var_dump($request);

    }
    
    
    
    
    //删除
    public function del($a)
    {
        $id =  $a;
        $res = DB::delete("delete from `about` where `id` = ?",[$id]);

        if ($res){
            echo '1';
        }else{
            echo '2';
        }
    }

    //编辑状态
    public function ajedit($a,$b)
    {
     //判断当期状态值
     $dis =  $b == 1?'2':'1';

     //获取当前id
     $id = $a;

     $res = DB::update("update `about` set  `a_display` = $dis where id = ?",[$id]);

         if ($res){
    
             return [1,$dis];
         }else{
    
             echo '2';
         }

    }

    //编辑查询
    public function edit($id)
    {
        $id = $id;

        $res = DB::select('select `id`,`a_icon`,`a_name`,`a_tag`,`a_module`,`a_display` from `about` where `id`=?',[$id]);
        //var_dump($res);
        if ($res){

            return view('admin/admabout/abedit',['res' => $res]);

        }else{

            echo '找不到';

            return back();
        }
    }


    //编辑执行
    public function update(Request $request)
    {

        $id = $request->id;



        $file = $request->File();

        //判断是否有头像上传
        if (!empty($file)){


            var_dump($file);
            $file = $request->File();
            $key = key($file);



            if ($_FILES[$key]['error'] > 0) {

                switch ($_FILES[$key]['error']) {
                    case '1':
                        $msg = '孩子, 回家重新上传, 文件太大了, 不要超过2M';
                        return back();
                        break;
                    case '2':
                        $msg = '请打开F12, 删除 MAX_FILE_SIZE , 即可正常上传';
                        return back();
                        break;
                    case '3':
                        $msg = '请查看网线是否连接好';
                        return back();
                        break;
                    case '4':
                        $msg = '请不要调戏我, 上传一个给我';
                        return back();
                        break;
                    case '6':
                        $msg = '请联系网管, 晚上啥时候约一下, 连个目录都不给我';
                        return back();
                        break;
                    case '7':
                        $msg = '再联系网管, 你想什么时候死, 说一声, 连个权限都不给我';
                        return back();
                        break;
                }
            }
            if (!is_uploaded_file($_FILES[$key]['tmp_name'])) {
                return back();
            }

            // 判断文件类型
            $ext = $request->file('a_icon')->getClientOriginalExtension();

            if (!in_array($ext, array('jpg', 'png', 'jpeg'))) {
                //('不合法的文件类型');
                return back();
            }

            // 移动文件到指定位置

            $path = './uploads/' . date('Y/m/d');
            $name = date('Ymd') . uniqid();
            $ext = $request->file('a_icon')->getClientOriginalExtension();
            $filename = $name . '.' . $ext;
            $request->file('a_icon')->move($path, $filename);

            //获取图片存储的路径
            $newfilename = $path.'/'.$filename;
            $arr = ltrim($newfilename,'.');

            $arr = array(
                'a_name' => $request->a_name,
                'a_tag' => $request->a_tag,
                'a_module' => $request->a_module,
                'a_display' => $request->a_dispaly,
                'a_icon' => $arr
            );

            $res = DB::table('about')->where('id',$id)->update($arr);

            if ($res){

                return redirect('/notice')->with(['message'=>'编辑成功','url' =>'adm/ab', 'jumpTime'=>3,'status'=>true]);

            }else{

                return redirect('/notice')->with(['message'=>'头像编辑失败','url' =>'adm/ab', 'jumpTime'=>3,'status'=>true]);
            }

        }else{


            //获取数据
            $arr = array(
                'a_name' => $request->a_name,
                'a_tag' => $request->a_tag,
                'a_module' => $request->a_module,
                'a_display' => $request->a_dispaly
            );

            $res = DB::table('about')->where('id',$id)->update($arr);

            if ($res){

                return redirect('/notice')->with(['message'=>'编辑成功','url' =>'adm/ab', 'jumpTime'=>3,'status'=>true]);

            }else{

                return redirect('/notice')->with(['message'=>'编辑失败','url' =>'adm/ab', 'jumpTime'=>3,'status'=>true]);
            }



        }




    }



    //前端分类数据查询
    public function fenclass()
    {

        //查询
        $res = DB::table('class')->get();


        return response()->json($res);
        //var_dump($res);
    }
    
}
